package router

import (
	"crow-docker/src/service"
	"github.com/gin-gonic/gin"
	"github.com/toolkits/pkg/logger"
	"strings"
)

// GetImageList 获取镜像列表
// @Summary 获取镜像列表
// @Description 获取镜像列表
// @Tags 镜像管理
// @Param ip_addr query string true "客户机id"
// @Param api_port query string true "客户机API端口"
// @Success 200 {object} response.Response{data=[]types.ImageSummary} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/docker/images [get]
// @Security Bearer
func GetImageList(c *gin.Context) {
	ipAddr := c.Query("ip_addr")
	dockerApiPort := c.Query("api_port")

	//链接docker
	dockerCli, err := service.ConnectDocker(ipAddr, dockerApiPort)
	if err != nil {
		logger.Error(err)
		SetErr(c, 500, err, err.Error())
		return
	}
	images, err := service.GetImagesList(dockerCli)
	if err != nil {
		logger.Error(err)
		SetErr(c, 500, err, err.Error())
		return
	}
	SetOK(c, images)
}

// GetImageInfo 查询指定镜像
// @Summary 查询指定镜像
// @Description 查询指定镜像
// @Tags 镜像管理
// @Param ip_addr query string true "客户机ip"
// @Param api_port query string true "docker-API端口"
// @Param image_name query string true "镜像名或镜像ID"
// @Success 200 {object} response.Response{data=types.ImageInspect} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/docker/image/info [get]
// @Security Bearer
func GetImageInfo(c *gin.Context) {
	ipAddr := c.Query("ip_addr")
	dockerApiPort := c.Query("api_port")
	imageName := c.Query("image_name")

	//链接docker
	dockerCli, err := service.ConnectDocker(ipAddr, dockerApiPort)
	if err != nil {
		logger.Error(err)
		SetErr(c, 500, err, err.Error())
		return
	}
	images, err := service.GetImage(dockerCli, imageName)
	if err != nil {
		logger.Error(err)
		SetErr(c, 500, err, err.Error())
		return
	}
	SetOK(c, images)
}

// PullImages 下载镜像
// @Summary 下载镜像
// @Description 批量下载镜像
// @Tags 镜像管理
// @Param ip_addr query string true "客户机ip"
// @Param api_port query string true "docker-API端口"
// @Param image_list query string true "镜像名列表，用逗号分隔"
// @Success 200 {object} response.Response{data=service.PullImageListResponse} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/docker/images/download [get]
// @Security Bearer
func PullImages(c *gin.Context) {
	ipAddr := c.Query("ip_addr")
	dockerApiPort := c.Query("api_port")
	imageListString := c.Query("image_list")

	imageList := strings.Split(imageListString, ",")
	//链接docker
	dockerCli, err := service.ConnectDocker(ipAddr, dockerApiPort)
	if err != nil {
		logger.Error(err)
		SetErr(c, 500, err, err.Error())
		return
	}

	pullImageListResponse, err := service.PullImages(dockerCli, imageList)
	if err != nil {
		logger.Error(err)
		SetErr(c, 500, err, err.Error())
		return
	}
	SetOK(c, pullImageListResponse)
}

// DeleteImage 删除镜像
// @Summary 删除镜像
// @Description 删除镜像
// @Tags 镜像管理
// @Param data body service.DeleteImageRequest  true "删除镜像的信息"
// @Success 200 {object} response.Response{data=[]types.ImageDeleteResponseItem} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/docker/image [delete]
// @Security Bearer
func DeleteImage(c *gin.Context) {
	var request service.DeleteImageRequest
	err := c.Bind(&request)
	if err != nil {
		SetErr(c, 500, err, err.Error())
		return
	}
	//链接docker
	dockerCli, err := service.ConnectDocker(request.IpAddr, request.ApiPort)
	if err != nil {
		logger.Error(err)
		SetErr(c, 500, err, err.Error())
		return
	}
	//删除指定镜像
	imageDeleteResponseItems, err := service.DeleteImage(dockerCli, request.ImageName)
	if err != nil {
		logger.Error(err)
		SetErr(c, 500, err, err.Error())
		return
	}
	SetOK(c, imageDeleteResponseItems)
}

// RenameImage 镜像tag
// @Summary 镜像tag
// @Description 添加一个镜像名+tag
// @Tags 镜像管理
// @Param data body  RenameImageNameRequest  true "操作信息"
// @Success 200 {object} response.Response{data=string} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/docker/image [put]
// @Security Bearer
func RenameImage(c *gin.Context) {
	var request RenameImageNameRequest
	err := c.Bind(&request)
	if err != nil {
		SetErr(c, 500, err, err.Error())
		return
	}
	//链接docker
	dockerCli, err := service.ConnectDocker(request.IpAddr, request.ApiPort)
	if err != nil {
		logger.Error(err)
		SetErr(c, 500, err, err.Error())
		return
	}

	err = service.AddNewTag(dockerCli, request.SourceImageName, request.NewImageName)
	if err != nil {
		logger.Error(err)
		SetErr(c, 500, err, err.Error())
		return
	}
	SetOK(c, "重命名成功")
}
